package com.autonavi.yunda.yunji.core.service;

import com.autonavi.yunda.yunji.common.exception.AmapPreconditions;
import com.autonavi.yunda.yunji.core.model.condition.DynamicConfigQueryCondition;
import com.autonavi.yunda.yunji.core.model.dto.DynamicConfigDTO;
import com.autonavi.yunda.yunji.core.model.mappers.DynamicConfigMapper;
import com.autonavi.yunda.yunji.core.vo.response.DynamicConfigPageVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author cizhong.wcs
 * @date 2022/9/2 16:54
 */
@Component
public class DynamicConfigService implements IDynamicConfigService {
    @Resource
    private DynamicConfigMapper dynamicConfigMapper;


    @Override
    public String fetchValue(String projectId, String keyName) {
        QueryWrapper<DynamicConfigDTO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("project_id", projectId);
        queryWrapper.eq("key_name", keyName);
        DynamicConfigDTO dto = dynamicConfigMapper.selectOne(queryWrapper);
        if (dto != null) {
            return dto.getValue();
        }
        return null;
    }

    public Long add(DynamicConfigDTO dto) {
        dynamicConfigMapper.insert(dto);
        return dto.getId();
    }

    public void update(UpdateWrapper<DynamicConfigDTO> updateWrapper) {
        dynamicConfigMapper.update(null, updateWrapper);
    }

    public void findByIdOrThrow(Long id) {
        DynamicConfigDTO dto = dynamicConfigMapper.selectById(id);
        AmapPreconditions.checkArgumentForUser(dto != null, "不存在该配置 id = " + id);
    }

    public List<DynamicConfigDTO> listAll(DynamicConfigQueryCondition condition) {
        return dynamicConfigMapper.selectList(getQueryWrapper(condition));
    }

    public DynamicConfigPageVO listPage(DynamicConfigQueryCondition condition) {
        Page<DynamicConfigDTO> page = new Page<>(condition.getPageNo(), condition.getPageSize());
        dynamicConfigMapper.selectPage(page, getQueryWrapper(condition));
        return new DynamicConfigPageVO(page.getRecords(), page.getTotal());
    }

    private QueryWrapper<DynamicConfigDTO> getQueryWrapper(DynamicConfigQueryCondition condition) {
        QueryWrapper<DynamicConfigDTO> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        if (StringUtils.isNotBlank(condition.getProjectId())) {
            wrapper.eq("project_id", condition.getProjectId());
        }
        if (StringUtils.isNotBlank(condition.getKeyName())) {
            wrapper.like("key_name", condition.getKeyName());
        }
        if (StringUtils.isNotBlank(condition.getValue())) {
            wrapper.like("value", condition.getValue());
        }
        if (StringUtils.isNotBlank(condition.getDescription())) {
            wrapper.like("description", condition.getDescription());
        }
        return wrapper;
    }
}
